<?php

namespace App\Http\Controllers;

use Illuminate\Routing\Controller as BaseController;
use Cache , Request , Response;
use Snoopy , simple_html_dom;

class CommonController extends BaseController
{



    function __construct()
    {

    }


    //登录前初始化
    function pre()
    {
        try
        {
            ini_set("max_execution_time" , 18000);

            $agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36';//伪装浏览器
    		$library_ip = "202.206.242.99";
    		$library_host = "http://202.206.242.99/";
    		$library_pic_router = "reader/captcha.php";
    		$library_login_router = "reader/login.php";

    		$snoopy = new Snoopy;
    		$submit_url = $library_host.$library_pic_router;

    		//获取cookie
    		$snoopy->host = $library_ip;
    		$snoopy->agent = $agent;
    		$snoopy->fetch($submit_url);
    		$snoopy->setcookies();
    		$cookie = $snoopy->cookies;
    		//超时返回
    		if ($snoopy->status != '200' || $snoopy->timed_out)
    	    	return Response::json( ['error'=>true , 'result'=>[] , 'message'=>'获取验证码失败'] , 200 );

    		$result = $snoopy->results;

    		$pic_data = "data:image/gif;base64,".base64_encode($result);

            $token = $rand = md5( time().mt_rand(0,1000) );

            Cache::put($token , $cookie['PHPSESSID'] , 300);

            $return_arr = array(
                'username' => '' ,
                'password' => '' ,
                'code' => '' ,
                'code_row' => $pic_data ,
                'token' => $token
                );

    		return Response::json( ['error'=>false , 'result'=>$return_arr , 'message'=>''] , 200 );
        }
        catch( Exception $e )
        {
            return Response::json( ['error'=>true , 'result'=>[] , 'message'=>'网络异常，请重试'] , 200 );
        }

    }



    //用户登录
    function login()
    {
        try
        {
            if( !Request::has('username') || !Request::has('password') || !Request::has('code') || !Request::has('token') )
            {
    	 		return Response::json(['error'=>true , 'result'=>[] , 'message'=>'缺少重要参数'] , 200);
            }

            $token = Request::get('token');
            if( !Cache::has($token) )
            {
                return Response::json(['error'=>true , 'result'=>[] , 'message'=>'身份信息已过期，请重新登录'] , 200);
            }
            $sessionid = Cache::get($token);

    	 	$username 	= Request::get('username');
    	 	$password 	= Request::get('password');
    	 	$code 		= Request::get('code');

    	 	$agent 					= 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36';//伪装浏览器
    		$library_ip 			= "202.206.242.99";
    		$library_host 			= "http://202.206.242.99/";
    		$library_login_router 	= "reader/redr_verify.php";

    		$submit_vars['number'] 	= $username;
    		$submit_vars['passwd'] 	= $password;
    		$submit_vars['captcha'] = $code;
    		$submit_vars['select'] 	= "cert_no";

    		$snoopy = new Snoopy;
    		$submit_url = $library_host.$library_login_router;

    		//获取cookie
    		$snoopy->host = $library_ip;
    		$snoopy->agent = $agent;
    		$snoopy->maxredirs = 0;
    		$snoopy->referer = 'http://202.206.242.99/reader/redr_verify.php';
    		$snoopy->cookies['PHPSESSID'] = $sessionid;

    		$snoopy->submit($submit_url , $submit_vars);

    		//超时返回
    		if( $snoopy->timed_out )
    			return Response::json(['error'=>true , 'result'=>[] , 'message'=>'登录超时(1)，请重试'] , 200);

    		if ($snoopy->status <= 0)
                return Response::json(['error'=>true , 'result'=>[] , 'message'=>'登录失败(2)，请重试'] , 200);

            //检查用户是否存在
    		if( stripos($snoopy->results, "对不起，密码错误，请查实！") )
            {
    			return Response::json(['error'=>true , 'result'=>[] , 'message' => "对不起，密码错误，请查实！"] , 200);
    		}else if( stripos($snoopy->results, "验证码错误(wrong check code)") )
            {
    			return Response::json(['error'=>true , 'result'=>[] , 'message' => "验证码错误(wrong check code)"] , 200);
    		}else if( stripos($snoopy->results, "读者已经注销") )
            {
    			return Response::json(['error'=>true , 'result'=>[] , 'message' => "读者已被图书馆注销"] , 200);
    		}else{
    			//检查是否其他登录异常
    			//获取个人证件信息
    			$snoopy2 = new Snoopy;
    			$snoopy2->host = "202.206.242.99";
    			$snoopy2->referer = "http://202.206.242.99/reader/redr_verify.php";
    			$snoopy2->agent = $agent;
    			$snoopy2->cookies['PHPSESSID'] = $sessionid;
    			$snoopy2->fetch("http://202.206.242.99/reader/redr_info.php");

    			$result2 = $snoopy2->results;
    			if( $snoopy2->timed_out )
                {
    				return Response::json( ['error'=>true , 'result'=>[] , 'message'=>'登录超时，请重试'] , 200 );
    			}
    			if( !stripos($snoopy2->results,"证件信息") )
                {
    				return Response::json( ['error'=>true , 'result'=>[] , 'message'=>'图书馆登录失败，请登录学校图书馆网页版核实错误后再使用拾光登录'] , 200 );
    			}

    			return Response::json(['error'=>false , 'result'=>[] , 'message'=>'登陆成功'] , 200);
    		}
        }
        catch ( Exception $e )
        {
            return Response::json(['error'=>true , 'result'=>[] , 'message'=>'网络异常请重试'] , 200);
        }

    }



    //用户简介
    function intro()
    {
        try
        {
            if( !Request::has('token') )
            {
    	 		return Response::json(['error'=>true , 'result'=>[] , 'message'=>'缺少重要参数'] , 200);
            }

            $token = Request::get('token');
            if( !Cache::has($token) )
            {
                return Response::json(['error'=>true , 'result'=>[] , 'message'=>'身份信息已过期，请重新登录'] , 200);
            }
            $sessionid = Cache::get($token);

            $agent 					= 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36';//伪装浏览器

            //检查是否其他登录异常
            //获取个人证件信息
            $snoopy2 = new Snoopy;
            $snoopy2->host = "202.206.242.99";
            $snoopy2->referer = "http://202.206.242.99/reader/redr_info.php";
            $snoopy2->agent = $agent;
            $snoopy2->cookies['PHPSESSID'] = $sessionid;
            $snoopy2->fetch("http://202.206.242.99/reader/redr_info_rule.php");

            $result2 = $snoopy2->results;
            if( $snoopy2->timed_out )
            {
                return Response::json( ['error'=>true , 'result'=>[] , 'message'=>'登录超时，请重试'] , 200 );
            }
            if( !stripos($snoopy2->results,"证件信息") )
            {
                return Response::json( ['error'=>true , 'result'=>[] , 'message'=>'图书馆登录失败，请登录学校图书馆网页版核实错误后再使用拾光登录'] , 200 );
            }

            $return_arr = array();

            $html2 = new simple_html_dom();
    	    $html2->load($result2);
    	    $book_nums = $html2->find("strong");

            $tds = $html2->find("td");

            //姓名
            $name_td = $tds[1]->innertext;
            $name_arr = explode("</span>", $name_td);
            $return_arr['name'] = $name_arr[1];

            //证件号
            $number = $tds[2]->innertext;
            $number_arr = explode("</span>", $number);
            $return_arr['number'] = $number_arr[1];

    	    //五天内即将超期
    		$return_arr['five'] = $book_nums[1]->innertext;

            //累计借书
    	    $all_books_count = $tds[12]->innertext;
    	    $all_books_count_arr = explode("</span>", $all_books_count);
    	    $all_books_count = $all_books_count_arr[1];
    		$return_arr['count'] = substr(trim($all_books_count) , 0 , 1);

            //欠款金额
    	    $own_money = $tds[14]->innertext;
    	    $own_money_arr = explode("</span>", $own_money);
    		$return_arr['own'] = $own_money_arr[1];

            //职称
            $caller = $tds[10]->innertext;
            $caller_arr = explode("</span>", $caller);
            $return_arr['type'] = $caller_arr[1];

            $rss_token = "";
    		$snoopy3 = new Snoopy;
    		$snoopy3->host = "202.206.242.99";
    		$snoopy3->agent = $agent;
    		$snoopy3->cookies['PHPSESSID'] = $sessionid;
    		$snoopy3->fetch("http://202.206.242.99/reader/redr_cust_result.php");
    		$result3 = $snoopy3->results;
    		if( !$snoopy3->timed_out ){
    			$temp_pos1 = strpos($result3 , "http://202.206.242.99:80/reader/rss.php?type=3&id=");
    			if( $temp_pos1 ){
    				$target_url_string = substr($result3 , $temp_pos1+50 , 48);
    				$return_arr['rss_token'] = $target_url_string;
    			}
    		}

            return Response::json( ['error'=>true , 'result'=>$return_arr , 'message'=>''] , 200 );
        }
        catch( Exception $e )
        {
            return Response::json(['error'=>true , 'result'=>[] , 'message'=>'网络异常，请重试'] , 200);
        }
    }


    //验证token是否过期
    function verify(){
        try
        {
            if( !Request::has('token') )
            {
    	 		return Response::json(['error'=>true , 'message'=>'缺少重要参数'] , 200);
            }

            $token = Request::get('token');
            if( !Cache::has($token) )
            {
                return Response::json(['error'=>true , 'message'=>'身份信息已过期，请重新登录'] , 200);
            }
            $sessionid = Cache::get($token);

            $agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36';//伪装浏览器

            //检查是否其他登录异常
            //获取个人证件信息
            $snoopy2 = new Snoopy;
            $snoopy2->host = "202.206.242.99";
            $snoopy2->referer = "http://202.206.242.99/reader/redr_verify.php";
            $snoopy2->agent = $agent;
            $snoopy2->cookies['PHPSESSID'] = $sessionid;
            $snoopy2->fetch("http://202.206.242.99/reader/redr_info.php");

            $result2 = $snoopy2->results;
            if( $snoopy2->timed_out || !stripos($snoopy2->results,"证件信息") )
            {
                ini_set("max_execution_time" , 18000);

        		$library_ip = "202.206.242.99";
        		$library_host = "http://202.206.242.99/";
        		$library_pic_router = "reader/captcha.php";
        		$library_login_router = "reader/login.php";

        		$snoopy = new Snoopy;
        		$submit_url = "$library_host.$library_pic_router";

        		//获取cookie
        		$snoopy->host = $library_ip;
        		$snoopy->agent = $agent;
        		$snoopy->fetch($submit_url);
        		$snoopy->setcookies();
        		$cookie = $snoopy->cookies;
        		//超时返回
        		if ($snoopy->status != '200' || $snoopy->timed_out)
        	    	return Response::json( ['error'=>true , 'message'=>'获取验证码失败'] , 200 );

        		$result = $snoopy->results;

        		$pic_data = "data:image/gif;base64,".base64_encode($result);

                $token = $rand = md5( time().mt_rand(0,1000) );

                Cache::put($token , $cookie['PHPSESSID'] , 300);

                $return_arr = array(
                    'username' => '' ,
                    'password' => '' ,
                    'code' => '' ,
                    'code_row' => $pic_data ,
                    'token' => $token
                    );

                return Response::json( ['error'=>true , 'result'=>$return_arr] , 200 );
            }

            return Response::json(['error'=>false , 'result'=>[] , 'message'=>'验证成功'] , 200);
        }
        catch( Exception $e )
        {
            return Response::json(['error'=>true , 'result'=>[] , 'message'=>'网络异常，请重试'] , 200);
        }
    }



    //获取在借的图书
    function inRentBook()
    {
        try
        {
            if( !Request::has('token') )
            {
    	 		return Response::json(['error'=>true , 'result'=>[] , 'message'=>'缺少重要参数'] , 200);
            }

            $token = Request::get('token');
            if( !Cache::has($token) )
            {
                return Response::json(['error'=>true , 'result'=>[] , 'message'=>'身份信息已过期，请重新登录'] , 200);
            }
            $sessionid = Cache::get($token);

            $agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36';//伪装浏览器
    		$library_ip = "202.206.242.99";
    		$library_referer = "http://202.206.242.99/reader/book_lst.php";

    		$snoopy2 = new Snoopy;
    		$snoopy2->host = $library_ip;
    		$snoopy2->referer = $library_referer;
    		$snoopy2->agent = $agent;
    		$snoopy2->cookies['PHPSESSID'] = $sessionid;

    		$target_url = "http://202.206.242.99/reader/book_lst.php";
    		$snoopy2->fetch($target_url);

    		$result = $snoopy2->results;

    		if( $snoopy2->timed_out || !strpos($result , "当前借阅") ){
    			$this->forgetSession();
    			return Response::json( ['error'=>true , 'result'=>[] , 'message'=>'请求超时'], 200);
    		}

    		$html = new simple_html_dom();
    		$html->load($result);

    		//获取所有在借书籍
    		$current_rent_arr = array();

    		$table_lines = $html->find(".table_line");
            if( count($table_lines) == 0 )
            {
                return Response::json( ['error'=>false , 'result'=>[] , 'message'=>'请求超时'], 200);
            }

            $table_line = $table_lines[0];
    		$table_line_count = count($table_line);

    		$all_trs = $table_line->find("tr");

    		for( $i=1 ; $i < count($all_trs) ; $i++ )
            {
    			$temp_tr = $all_trs[$i];
    			$temp_tds = $temp_tr->find("td");

    			//分析详情连接
    			$temp_as = $temp_tds[1]->find("a");
    			if( count($temp_as) > 0 )
                {
    				$temp_as_href = $temp_as[0]->href;
    				$target_detail_url = substr($temp_as_href , 8);
    			}else{
    				$target_detail_url = "";
    			}

    			//分析此借阅书的状态
    			$book_state = "0";
    			$temp_return = $temp_tds[3];
    			$temp_font = $temp_return->find("font")[0]->color;
    			if( $temp_font == 'red' ){
    				$book_state = 1;
    			}

    			$renew_input = $temp_tr->find("input");
    			if( count($renew_input) == 0 )
                {
    				$temp_renew_code = "";
    				$bar_code = "";
    			}else{
    				$getLib = $renew_input[0]->onclick;
    				$checkCode_arr = explode(",", $getLib);
    				$bar_code = substr($checkCode_arr[0] , 10 , 7);
    				$checkCode = substr($checkCode_arr[1] , 1 , 8);
    				$temp_renew_code = $checkCode;
    			}
    			$current_rent_arr[] = array(
    				'num' => $temp_tds[0]->innertext ,
    				'name' => $temp_tds[1]->innertext ,
    				'href' => $target_detail_url ,
    				'rent_time' => trim($temp_tds[2]->innertext) ,
    				'return_time' => trim($temp_tds[3]->innertext) ,
    				'con_num' => $temp_tds[4]->innertext ,
    				'location' => $temp_tds[5]->innertext ,
    				'attach' => $temp_tds[6]->innertext ,
    				'renew_code' => $temp_renew_code ,
    				'bar_code' => $bar_code ,
    				'state' => $book_state
    				);
    		}

    		return Response::json( ['error'=>false , 'result'=>$current_rent_arr , 'message'=>''] , 200 );
        }
        catch( Exception $e )
        {
            return Response::json(['error'=>true , 'result'=>[] , 'message'=>'网络异常，请重试'] , 200);
        }
    }


    //搜索图书api
    function search()
    {
        try
        {
            // if( !Request::has('content') || strlen(Request::get('content')) == 0 || !Request::has('token') ){
    		// 	return Response::json( ['error'=>true , 'result'=>[] , 'message'=>'搜索内容不能为空'] );
    		// }
            //
            // $token = Request::get('token');
            // if( !Cache::has($token) )
            // {
            //     return Response::json(['error'=>true , 'result'=>'tokenNull' , 'message'=>'身份信息已过期，请重新登录'] , 200);
            // }
            // $sessionid = Cache::get($token);

    		$content 	= Request::get('content');
    		$page 		= Request::has('page') ? Request::get('page') : 1;
    		$button 	= Request::get('button');

    		$this->data['content'] = rawurldecode($content);

    		$agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36';//伪装浏览器
    		$library_ip = "202.206.242.99";
    		$library_referer = "http://202.206.242.99/opac/search.php";

    		$snoopy2 = new Snoopy;
    		$snoopy2->host = $library_ip;
    		$snoopy2->referer = $library_referer;
    		$snoopy2->agent = $agent;
    		//$snoopy2->cookies['PHPSESSID'] = $sessionid;

    		$search_url = "http://202.206.242.99/opac/openlink.php";
    		$search_url .= "?strSearchType=title";
    		$search_url .= "&match_flag=forward";
    		$search_url .= "&historyCount=1";
    		$search_url .= "&strText=".urlencode($content);
    		$search_url .= "&doctype=ALL";
    		$search_url .= "&displaypg=20";
    		$search_url .= "&showmode=list";
    		$search_url .= "&sort=CATA_DATE";
    		$search_url .= "&orderby=desc";
    		$search_url .= "&location=ALL";
    		$search_url .= "&page=".$page;

    		$snoopy2->fetch($search_url);
    		$result2 = $snoopy2->results;

    		if( $snoopy2->timed_out || !strpos($result2, "所有图书") ){
    			$this->data['search_error'] = 1;
    			$this->data['search_error_msg'] = "搜索失败";
    		}

    		//分析搜索内容
    		$search_result_arr = array();

    		$html = new simple_html_dom();
    		$html->load($result2);

    		$books_list = $html->find(".book_list_info");
    		for( $i=0 ; $i<count($books_list) ; $i++ ){
    			$temp_list_info = $books_list[$i];
    			//书名
    			$book_name = $temp_list_info->find("a")[0]->innertext;
    			$book_name_arr = explode('.',$book_name);
    			$temp_key = ($page-1)*20+$i+1;
    			$book_name_arr[0] = $temp_key;
    			$book_name = implode('.' , $book_name_arr);

    			$book_index = $temp_list_info->find("h3")[0]->innertext;
    			$temp_pos = strpos($book_index , "</a>");
    			//书索引
    			$book_index = substr($book_index , $temp_pos+4);
    			//书籍详情链接
    			$book_href = $temp_list_info->find("a")[0]->href;
    			//书类型
    			$book_type = $temp_list_info->find("span")[0]->innertext;

    			$rent_detail = $temp_list_info->find("span")[1]->innertext;
    			$rent_detail_arr = explode("<br>", $rent_detail);
    			//馆藏复本
    			$collect_num = $rent_detail_arr[0];
    			//可借复本
    			$rent_num = $rent_detail_arr[1];

    			$all_detail = $temp_list_info->find("p")[0]->innertext;
    			$all_detail_temp = explode("</span>" , $all_detail);
    			$all_detail_temp_arr = explode("<br />" , $all_detail_temp[1]);
    			//作者
    			$book_author = $all_detail_temp_arr[0];
    			//出版社
    			$book_press = $all_detail_temp_arr[1];

    			$search_result_arr[] = array(
    				'name' => $book_name ,
    				'index' => $book_index ,
    				'href' => $book_href ,
    				'author' => $book_author ,
    				'press' => $book_press ,
    				'type' => $book_type ,
    				'collect' => trim($collect_num) ,
    				'rent' => trim($rent_num)
    				);
    		}

    		//查找分页信息
    		$page_info = $html->find(".pagination")[0];
    		$all_fonts = $page_info->find("font");

    		$currentPage = isset($all_fonts[0]) ? $all_fonts[0]->innertext : 1;

    		$allPage = isset($all_fonts[1]) ? $all_fonts[1]->innertext : 1;

    		$page_arr = array(
    			'current' => $currentPage ,
    			'all' => $allPage
    			);
            $result = array(
                'result'=>$search_result_arr ,
                'page'=>$page_arr ,
                'content' => $content
                );

    		return Response::json( ['error'=>false , 'result'=>$result , 'message'=>''] , 200 );
        }
        catch( Exception $e )
        {
            return Response::json(['error'=>true , 'result'=>[] , 'message'=>'网络异常，请重试'] , 200);
        }
    }



    //图书详情
    function detail()
    {
        try
        {
            // if( !Request::has('token') )
            // {
    		// 	return Response::json( ['error'=>true , 'result'=>[] , 'message'=>'缺少重要参数'] );
    		// }
            //
            // $token = Request::get('token');
            // if( !Cache::has($token) )
            // {
            //     return Response::json(['error'=>true , 'result'=>[] , 'message'=>'身份信息已过期，请重新登录'] , 200);
            // }
            // $sessionid = Cache::get($token);

            if( !Request::has('href') || strlen(Request::get('href')) <= 0 )
            {
    			$this->data['detail_error'] = 1;
    			$this->data['detail_error_msg'] = '目标图书不能为空';
    		}
    		$href = Request::get('href');
    		$mac_no = "";
    		$href_arr = explode('=' , $href);
    		if( count($href_arr) < 2 )
            {
    			$this->data['detail_error'] = 1;
    			$this->data['detail_error_msg'] = '目标图书记录号错误';
    		}else{
    			$mac_no = $href_arr[1];
    		}

    		//查找是否已收藏
    		//$mac_no = $mac_no;
    		$temp_no = $mac_no;
    		$temp_no_length = strlen($temp_no);
    		$temp_no_attach = "";
    		if( $temp_no_length < 10 )
            {
    			$split = 10 - $temp_no_length;
    			for( $i=1 ; $i<=$split ; $i++ )
                {
    				$temp_no_attach .= '0';
    			}
    			$temp_no = $temp_no_attach.$temp_no;
    		}
    		$mac_no = $temp_no;


    		$agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36';//伪装浏览器
    		$library_ip = "202.206.242.99";
    		$library_referer = "http://202.206.242.99/opac/search.php";

    		$snoopy2 = new Snoopy;
    		$snoopy2->host 		= $library_ip;
    		$snoopy2->referer 	= $library_referer;
    		$snoopy2->agent 	= $agent;
    		//$snoopy2->cookies['PHPSESSID'] = $sessionid;

    		$target_url = "http://202.206.242.99/opac/".$href;
    		$snoopy2->fetch($target_url);

    		$result = $snoopy2->results;

    		if( $snoopy2->timed_out || !strpos($result , "书目信息") ){
    			//$this->forgetSession();
    			$this->data['detail_error'] = 1;
    			$this->data['detail_error_msg'] = "未搜索到数目信息";
    		}

    		$html = new simple_html_dom();
    		$html->load($result);

    		$all_dds = $html->find("dd");
    		$all_dts = $html->find("dt");

    		if( count($all_dds) != count($all_dts) )
    			return Response::json( ['error'=>true , 'result'=>[] , 'message'=>'书籍详情信息出错'] , 200 );

    		//详情条目列表
    		$detail_list = array();
    		$detail_list['author'] = "";
    		$detail_list['title'] = "";
    		$detail_list['press'] = "";
    		$detail_list['brief'] = "";
    		$detail_list['mac_no'] = $mac_no;

    		$detail_list_count = count($all_dts);
    		for( $i=0 ; $i < $detail_list_count ; $i++ ){
    			$temp_dt = $all_dts[$i]->innertext;
    			$temp_dd = $all_dds[$i]->innertext;

    			if( strpos($temp_dt , "名/") ){
    				$detail_list['title'] = $temp_dd;
    			}
    			if( strpos($temp_dt , "版发行") ){
    				$detail_list['press'] = $temp_dd;
    			}
    			if( strpos($temp_dt , "责任者") && !strpos($temp_dt , "/责任者") ){
    				$detail_list['author'] .= ' '.$temp_dd;
    			}
    			if( strpos($temp_dt , "文摘附注") ){
    				$detail_list['brief'] = $temp_dd;
    			}
    		}

    		//获取图片
    		$temp_pos = strpos($html , "ajax_douban.php?isbn=");
    		if( $temp_pos ){
    			$temp_isbn = substr($html , $temp_pos+21 , 13);
    			$pic_data_addr = "http://202.206.242.99/opac/ajax_douban.php?isbn=".$temp_isbn;

    			$snoopy5 = new Snoopy;
    			$snoopy5->host = $library_ip;
    			$snoopy5->agent = $agent;
    			//$snoopy5->cookies['PHPSESSID'] = $sessionid;

    			$snoopy5->fetch($pic_data_addr);
    			$result5 = $snoopy5->results;
    			if( $snoopy2->timed_out || !strpos($result5 , "image") ){
    				$pic_data_addr = "../../img/noimage.jpg";
    			}else{
    				$result5_arr = json_decode($result5 , true);
    				if( !isset($result5_arr['image']) ){
    					$pic_data_addr = "../../img/noimage.jpg";
    				}else{
    					if( strpos($result5_arr['image'], "nobook") ){
    						$pic_data_addr = "../../img/noimage.jpg";
    					}else{
    						$pic_data_addr = $result5_arr['image'];
    					}
    				}
    			}
    		}else{
    			$pic_data_addr = "../../img/noimage.jpg";
    		}
    		$detail_list['pic'] = $pic_data_addr;

    		//可借阅信息表格
    		$detail_table = array();
    		$rent_detail_table = $html->find("#item")[0];
    		$all_trs = $rent_detail_table->find("tr");
    		for( $k=1 ; $k < count($all_trs) ; $k++ ){
    			$temp_tr = $all_trs[$k];
    			$temp_all_tds = $temp_tr->find("td");
    			$detail_table[] = array(
    				'index' => $temp_all_tds[0]->innertext ,
    				'num' => $temp_all_tds[1]->innertext ,
    				'year' => $temp_all_tds[2]->innertext ,
    				'location' => trim($temp_all_tds[3]->innertext) ,
    				'state' => trim($temp_all_tds[4]->innertext) ,
    				);
    		}

    		//查找相关借阅和相关收藏信息信息
    		$target_ul = $html->find("#tabs2")[0]->find("ul");
    		if( count($target_url) == 0 ){
    			$this->data['related_rent_url'] = "";
    			$this->data['related_collect_url'] = "";
    		}else{
    			$target_lis = $target_ul[0]->find("li");
    			if( count($target_lis) == 0 ){
    				$this->data['related_rent_url'] = "";
    				$this->data['related_collect_url'] = "";
    			}else{
    				$temp_rent_url = $target_lis[4]->find("a")[0]->getAttribute("href");
    				$temp_collect_url = $target_lis[4]->find("a")[0]->getAttribute("href");
    				$temp_rent_arr = explode('?',$temp_rent_url);
    				if( count($temp_rent_arr) == 0 ){
    					$this->data['related_rent_url'] 	= "";
    				}else{
    					$this->data['related_rent_url'] 	= $temp_rent_arr[1];
    				}
    				$temp_collect_arr = explode('?',$temp_collect_url);
    				if( count($temp_collect_arr) == 0 ){
    					$this->data['related_collect_url'] 	= "";
    				}else{
    					$this->data['related_collect_url'] 	= $temp_collect_arr[1];
    				}
    			}
    		}

    		$this->data['detail_list'] = $detail_list;
    		$this->data['detail_table'] = $detail_table;

    		return Response::json(['error'=>false , 'result'=>[] , 'result'=>$this->data] , 200);
        }
        catch( Exception $e )
        {
            return Response::json(['error'=>true , 'result'=>[] , 'message'=>'网络异常，请重试'] , 200);
        }
    }



    //续借验证码
    function reNewPic()
    {
        try
        {
            if( !Request::has('token') )
            {
    	 		return Response::json(['error'=>true , 'result'=>[] , 'message'=>'缺少重要参数'] , 200);
            }
            if( !Request::has('bar_code') || !Request::has('check') || !Request::has('captcha') )
            {
    	 		return Response::json(['error'=>true , 'result'=>[] , 'message'=>'缺少书籍相关参数'] , 200);
            }

            $token = Request::get('token');
            if( !Cache::has($token) )
            {
                return Response::json(['error'=>true , 'result'=>[] , 'message'=>'身份信息已过期，请重新登录'] , 200);
            }
            $sessionid = Cache::get($token);

            ini_set("max_execution_time" , 18000);

    		$agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36';//伪装浏览器
    		$library_ip = "202.206.242.99";
    		$library_host = "http://202.206.242.99/";
    		$library_pic_router = "reader/captcha.php";

    		$snoopy = new Snoopy;
    		$submit_url = $library_host.$library_pic_router;

    		$snoopy->host = $library_ip;
    		$snoopy->agent = $agent;
    		$snoopy->referer = "http://202.206.242.99/reader/book_lst.php";
    		$snoopy->cookies['PHPSESSID'] = $sessionid;
    		$snoopy->fetch($submit_url);

    		//超时返回
    		if ($snoopy->status != '200' || $snoopy->timed_out)
    	    	return Response::json( ['error'=>true , 'message'=>'获取验证码失败'] , 200 );

    		$result = $snoopy->results;
    		$pic_data = "data:image/gif;base64,".base64_encode($result);

    		return Response::json( ['error'=>false , 'result'=>$pic_data , 'message'=>''] , 200 );
        }
        catch( Exception $e )
        {
            return Response::json(['error'=>true , 'result'=>[] , 'message'=>'网络异常，请重试'] , 200);
        }

    }



    //续借
    function reNew()
    {
        try
        {
            if( !Request::has('token') || !Request::has('bar_code') || !Request::has('check') || !Request::has('captcha') )
            {
    	 		return Response::json(['error'=>true , 'result'=>[] , 'message'=>'缺少重要参数'] , 200);
            }

            $token = Request::get('token');
            if( !Cache::has($token) )
            {
                return Response::json(['error'=>true , 'result'=>[] , 'message'=>'身份信息已过期，请重新登录'] , 200);
            }
            $sessionid = Cache::get($token);

    		$bar_code 	= Request::get('bar_code');
    		$check 		= Request::get('check');
    		$captcha 	= Request::get('captcha');
    		$time = time();

    		$agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36';//伪装浏览器
    		$library_ip = "202.206.242.99";
    		$library_host = "http://202.206.242.99/";
    		$library_renew_router = "reader/ajax_renew.php";

    		$snoopy = new Snoopy;
    		$renew_url = $library_host.$library_renew_router;
    		$renew_url .= "?bar_code=".$bar_code;
    		$renew_url .= "&check=".$check;
    		$renew_url .= "&captcha=".$captcha;
    		$renew_url .= "&time=".$time;

    		$snoopy->host = $library_ip;
    		$snoopy->agent = $agent;
    		$snoopy->cookies['PHPSESSID'] = $sessionid;
    		$snoopy->fetch($renew_url);

    		//超时返回
    		if ($snoopy->status != '200' || $snoopy->timed_out)
    	    	return Response::json( ['error'=>true , 'result'=>[] , 'message'=>'续借失败'] , 200 );

    		$result = $snoopy->results;

    		return Response::json( ['error'=>false , 'result'=>$result , 'message'=>''] , 200 );
        }
        catch( Exception $e )
        {
            return Response::json(['error'=>true , 'result'=>[] , 'message'=>'网络异常，请重试'] , 200);
        }

    }



    //历史借阅
    function allRent()
    {
        try
        {
            if( !Request::has('token') || !Request::has('page') || Request::get('page') <= 0 )
            {
    	 		return Response::json(['error'=>true , 'result'=>[] , 'message'=>'缺少重要参数'] , 200);
            }

            $token = Request::get('token');
            if( !Cache::has($token) )
            {
                return Response::json(['error'=>true , 'result'=>[] , 'message'=>'身份信息已过期，请重新登录'] , 200);
            }
            $sessionid = Cache::get($token);

    		$page = Request::get('page');

    		$agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36';//伪装浏览器
    		$library_ip = "202.206.242.99";
    		$library_host = "http://202.206.242.99/";
    		$library_hist_router = "reader/book_hist.php";

    		$snoopy = new Snoopy;
    		$hist_url = "http://202.206.242.99/reader/book_hist.php";
    		$hist_url .= "?page=".$page;

    		$snoopy->host = $library_ip;
    		$snoopy->agent = $agent;
    		$snoopy->cookies['PHPSESSID'] = $sessionid;
    		$snoopy->fetch($hist_url);

    		//超时返回
    		if ($snoopy->status != '200' || $snoopy->timed_out){
    			$this->forgetSession();
    	    	return Response::json( ['error'=>true , 'result'=>[] , 'message'=>'请求超时'] , 200 );
    		}

    	    $result = $snoopy->results;

    	    $html = new simple_html_dom();
    	    $html->load($result);

    	    $hist_arr = array();
    	    $table_lines = $html->find(".table_line");
            if( count($table_lines) == 0 )
            {
                return Response::json( ['error'=>false , 'result'=>[] , 'page'=>['current'=>1 , 'all'=>1] , 'message'=>''] , 200 );
            }

            $table_line = $table_lines[0];
    	    $all_trs = $table_line->find("tr");

    	    for( $i=1 ; $i<count($all_trs) ; $i++ ){
    	    	$temp_tr = $all_trs[$i];
    	    	$all_tds = $temp_tr->find("td");
    	    	//分析详情连接
    			$temp_as = $all_tds[2]->find("a");
    			if( count($temp_as) > 0 ){
    				$temp_as_href = $temp_as[0]->href;
    				$target_detail_url = substr($temp_as_href , 8);
    			}else{
    				$target_detail_url = "";
    			}
    	    	//条码
    	    	$num = $all_tds[1]->innertext;
    	    	//标题
    	    	$title = $all_tds[2]->innertext;
    	    	//作者
    	    	$author = $all_tds[3]->innertext;
    	    	//借阅日期
    	    	$rent_time = $all_tds[4]->innertext;
    	    	//归还日期
    	    	$return_time = $all_tds[5]->innertext;
    	    	//馆藏地
    	    	$location = $all_tds[6]->innertext;

    	    	$hist_arr[] = array(
    	    		'num' => $num ,
    	    		'href' => $target_detail_url ,
    	    		'title' => $title ,
    	    		'author' => $author ,
    	    		'rent_time' => $rent_time ,
    	    		'return_time' => $return_time ,
    	    		'location' => $location
    	    		);
    	    }

    	    //查找分页数据
    	    $all_pages = $html->find("#f")[0];
    	    $all_fonts = $all_pages->find("font");
    	    $page_arr = array(
    	    	'current' => count($all_fonts) > 0 ? $all_fonts[0]->innertext : 1 ,
    	    	'all' => count($all_fonts) > 1 ? $all_fonts[1]->innertext : 1
    	    	);

    	    return Response::json( ['error'=>false , 'result'=>$hist_arr , 'page'=>$page_arr , 'message'=>''] , 200 );
        }
        catch( Exception $e )
        {
            return Response::json(['error'=>true , 'result'=>[] , 'message'=>'网络异常，请重试'] , 200);
        }
    }



    //相关借阅
    function related()
    {
        try
        {
            if( !Request::has('token') || !Request::has('url') || !Request::has('type') || !Request::has('call_no') )
            {
    	 		return Response::json(['error'=>true , 'result'=>[] , 'message'=>'缺少重要参数'] , 200);
            }

            $token = Request::get('token');
            if( !Cache::has($token) )
            {
                return Response::json(['error'=>true , 'result'=>[] , 'message'=>'身份信息已过期，请重新登录'] , 200);
            }
            $sessionid = Cache::get($token);

    		$origin_marc = Request::get('url');
    		if( $origin_marc == "" )
            {
    			return Response::json(['error'=>true , 'result'=>[] , 'message'=>'无效请求地址'] , 200);
    		}

            $url = Request::get('url')."&call_no=".Request::get('call_no');
    		$type = Request::get('type') > 0 ? 1 : 0;

    		$target_url = "";
    		switch ($type) {
    			case 0:
    				$target_url = "ajax_lend_related.php?".$url;
    				break;
    			case 1:
    				$target_url = "ajax_shelf_related.php?".$url;
    				break;
    			default:
    				break;
    		}


    		$snoopy2 = new Snoopy;
    		$snoopy2->host = "202.206.242.99";
    		$snoopy2->referer = "http://202.206.242.99/opac/item.php?".$origin_marc;
    		$snoopy2->agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36";
    		$snoopy2->cookies['PHPSESSID'] = $sessionid;

    		$target_url = "http://202.206.242.99/opac/".$target_url;
    		$snoopy2->fetch($target_url);
    		$result_arr = array();
    		if( !$snoopy2 || $snoopy2->timed_out ){
    			return Response::json(['error'=>true , 'result'=>[] , 'message'=>'请求失败'] , 200);
    		}else{
    			$html = new simple_html_dom();
    			$html->load($snoopy2->results);
    			$trs = $html->find("tr");
    			if( count($trs) <= 1 ){
    				return Response::json(['error'=>false , 'result'=>$result_arr] , 200);
    			}else{
    				for( $j=1 ; $j<count($trs) ; $j++ ){
    					$temp_tds = $trs[$j]->find("td");
    					if( count($temp_tds) > 3 ){
    						$temp_arr = array(
    							'name' => $temp_tds[0]->innertext ,
    							'author' => $temp_tds[1]->innertext ,
    							'press' => $temp_tds[2]->innertext ,
    							'index' => $temp_tds[3]->innertext
    							);
    						$result_arr[] = $temp_arr;
    					}
    				}
    				return Response::json(['error'=>false , 'result'=>$result_arr , 'message'=>''] , 200);
    			}
    		}
        }
        catch( Exception $e )
        {
            return Response::json(['error'=>true , 'result'=>[] , 'message'=>'网络异常，请重试'] , 200);
        }
    }


    function test(){
        $token = "6232c667cdb746af0bb8ffecb566d063";
        if( Cache::has($token) ){
            echo Cache::get($token);
        }else{
            echo '0';
        }
    }



    function updateInfo()
    {
        $return = array();

        if( !Request::has('token') )
        {
            return Response::json(['error'=>true , 'result'=>'tokenNull' , 'message'=>'缺少重要参数'] , 200);
        }

        $token = Request::get('token');
        if( !Cache::has($token) )
        {
            return Response::json(['error'=>true , 'result'=>'tokenTimeOut' , 'message'=>'身份信息已过期，请重新登录1'] , 200);
        }
        $sessionid = Cache::get($token);

		//$user_id = Session::get('user_id');
        //$user_id = 2;

		$agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36';//伪装浏览器
	    $library_ip = "202.206.242.99";
	    $library_host = "http://202.206.242.99/";
	    $library_pic_router = "reader/redr_info.php";
	    $library_referer = "http://202.206.242.99/reader/redr_verify.php";

	    $indexInfoUrl = $library_host.$library_pic_router;

		//user表更新数组
		$update_arr = array();

	    //获取个人证件信息
	    $snoopy = new Snoopy;
	    $snoopy->host = $library_ip;
	    $snoopy->referer = $library_referer;
	    $snoopy->agent = $agent;
	    $snoopy->cookies['PHPSESSID'] = $sessionid;
	    $snoopy->fetch($indexInfoUrl);

	    $result = $snoopy->results;

	    if( $snoopy->timed_out ){
			return Response::json(['error'=>true , 'result'=>'tokenTimeOut' , 'message'=>'身份信息已过期，请重新登录2'] , 200);
		}

	    if( !stripos($snoopy->results,"证件信息") ){
		    return Response::json(['error'=>true , 'result'=>'tokenTimeOut' , 'message'=>'身份信息已过期，请重新登录3'] , 200);
		}

	    $html = new simple_html_dom();
	    $html->load( $result );
	    //用户姓名
	    $info_name = $html->find(".profile-name")[0]->innertext;

	    $info_detail = $html->find(".profile-info-value");
	    //证件开始时间
	    $info_detail_start_time = $info_detail[0]->children[0]->innertext;
	    //证件结束时间
	    $info_detail_end_time = $info_detail[1]->children[0]->innertext;
	    //email地址
	    $info_detail_email = $info_detail[2]->children[0]->innertext;

	    $info_data_number = $html->find(".infobox-data-number");

	    //超期图书数量
	    $info_data_outdate = $info_data_number[0]->innertext;
		$return_arr['out'] = $info_data_outdate;
		$update_arr['out'] = $info_data_outdate;
	    //委托到书数量
	    $info_data_let = $info_data_number[1]->innertext;
	    //预约到书数量
	    $info_data_order = $info_data_number[2]->innertext;
	    //荐购图书数量
	    $info_data_recom = $info_data_number[3]->innertext;

	    //超过多少人
	    $info_overpeople_num = $html->find(".Num")[0]->innertext;
	    $update_arr['over_num'] = $info_overpeople_num;

	    //获得称号
	    $info_level = $html->find("em")[0]->innertext;
	    $update_arr['level'] = $info_level;

	    $return_arr = array(
	 	   'name' => $info_name ,
	 	   'over_num' => $info_overpeople_num ,
		   'level' => $info_level ,
		   'range' => 'http://202.206.242.99/tpl/reader/chartPie.htm' ,
		   'time' => 'http://202.206.242.99/tpl/reader/chartColumn.htm',
		   );


	    //获取证件信息
	    $snoopy2 = new Snoopy;
	    $snoopy2->host = $library_ip;
	    $snoopy2->referer = "http://202.206.242.99/reader/redr_info.php";
	    $snoopy2->agent = $agent;
	    $snoopy2->cookies['PHPSESSID'] = $sessionid;
	    $snoopy2->fetch("http://202.206.242.99/reader/redr_info_rule.php");
	    $result2 = $snoopy2->results;

	    if( $snoopy2->timed_out || !stripos($result2,"证件信息") ){
	 	    $over_to_five = "";
		    $all_books_count = "";
		    $own_money = "";
	    }else{
		    $html2 = new simple_html_dom();
		    $html2->load($result2);
		    $book_nums = $html2->find("strong");

		    //五天内即将超期
		    $over_to_five = $book_nums[1]->innertext;
			$update_arr['five'] = $over_to_five;
		    //累计借书
		    $all_books_count = $html2->find("td")[12]->innertext;
		    //$temp_pos = stripos($all_books_count,"</span>");
		    $all_books_count_arr = explode("</span>", $all_books_count);
		    $all_books_count = $all_books_count_arr[1];
		    $all_books_count = str_replace('册次' , '' , $all_books_count);
			$update_arr['count'] = $all_books_count;
		    //欠款金额
		    $own_money = $html2->find("td")[14]->innertext;
		    // $temp_pos = stripos($own_money,"</span>");
		    // $own_money = substr($own_money[1] , $temp_pos+7);
		    $own_money_arr = explode("</span>", $own_money);
		    $own_money = $own_money_arr[1];
			$update_arr['own'] = $own_money;
	    }

        $return['return_arr'] = $return_arr;
        $return['update_arr'] = $update_arr;

		//获取所有在借书籍
		$current_rent_arr = array();

		$snoopy2 = new Snoopy;
		$snoopy2->host = "202.206.242.99";
		$snoopy2->referer = "http://202.206.242.99/reader/book_lst.php";
		$snoopy2->agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36";
		$snoopy2->cookies['PHPSESSID'] = $sessionid;

		$target_url = "http://202.206.242.99/reader/book_lst.php";
		$snoopy2->fetch($target_url);

		$result = $snoopy2->results;

		if( $snoopy2->timed_out || !strpos($result , "当前借阅") ){
            return Response::json(['error'=>true , 'result'=>'tokenTimeOut' , 'message'=>'身份信息已过期，请重新登录4'] , 200);
		}

		$html = new simple_html_dom();
		$html->load($result);
		$table_lines = $html->find(".table_line");
		if( count($table_lines) > 0 ){
			$table_line = $table_lines[0];
			$table_line_count = count($table_line);

			$all_trs = $table_line->find("tr");
			for( $i=1 ; $i < count($all_trs) ; $i++ ){
				$temp_tr = $all_trs[$i];
				$temp_tds = $temp_tr->find("td");

				//分析详情连接
				$temp_as = $temp_tds[1]->find("a");
				if( count($temp_as) > 0 ){
					$temp_as_href = $temp_as[0]->href;
					$target_detail_url = substr($temp_as_href , 8);
				}else{
					$target_detail_url = "";
				}

				//分析此借阅书的状态
				$book_state = "0";
				$temp_return = $temp_tds[3];
				$temp_font = $temp_return->find("font")[0]->color;
				if( $temp_font == 'red' ){
					$book_state = 1;
				}

				$renew_input = $temp_tr->find("input");
				if( count($renew_input) == 0 ){
					$temp_renew_code = "";
					$bar_code = "";
				}else{
					$getLib = $renew_input[0]->getAttribute("onclick");
					$checkCode_arr = explode(",", $getLib);
					$bar_code = substr($checkCode_arr[0] , 10 , 7);
					$checkCode = substr($checkCode_arr[1] , 1 , 8);
					$temp_renew_code = $checkCode;
				}
				$return_time = "";
				$temp_fonts = $temp_tds[3]->find("font");
				if( count($temp_fonts) > 0 ){
					$return_time = trim($temp_fonts[0]->innertext);
				}else{
					$return_time = trim($temp_tds[3]->innertext);
				}

				$current_rent_arr[] = array(
					'num' => $temp_tds[0]->innertext ,
					'name' => $temp_tds[1]->innertext ,
					'href' => $target_detail_url ,
					'rent_time' => trim($temp_tds[2]->innertext) ,
					'return_time' => $return_time ,
					'con_num' => $temp_tds[4]->innertext ,
					'location' => $temp_tds[5]->innertext ,
					'attach' => $temp_tds[6]->innertext ,
					'renew_code' => $temp_renew_code ,
					'bar_code' => $bar_code ,
					'state' => $book_state
					);
			}
		}


		if( count($current_rent_arr) > 0 ){
			$insert_arr = array();
			//DB::table('user_rent')->where('user_id','=',$user_id)->delete();
			foreach( $current_rent_arr as $key => $val ){
				$insert_arr[] = array(
					'book_name' => $val['name'] ,
					'book_code' => $val['bar_code'] ,
					'book_href' => $val['href'] ,
					'book_location' => $val['location'] ,
					'rerent_num' => $val['con_num'] ,
					'rent_time' => $val['rent_time'] ,
					'return_time' => $val['return_time'] ,
					'attach' => $val['attach'] ,
					'bar_code' => $val['bar_code'] ,
					'check_code' => $val['renew_code'] ,
					'state' => $val['state']
					);
			}
			//DB::table('user_rent')->insert($insert_arr);
            $return['current_rent_arr'] = $insert_arr;
        }

	    $return['return_arr']['five']    = $over_to_five;
	    $return['return_arr']['count']   = $all_books_count;
	    $return['return_arr']['own']     = $own_money;
	    //DB::table('user')->where('id','=',$user_id)->update($return_arr);

		//if( $all_books_count > $pre_all_books ){
        if( true ){
			//查找历史借阅书籍
			$snoopy = new Snoopy;
			$hist_url = "http://202.206.242.99/reader/book_hist.php";
			//$hist_url .= "?page=1";

			$snoopy->host = "202.206.242.99";
			$snoopy->agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36";
			$snoopy->cookies['PHPSESSID'] = $sessionid;
			$submit_vars = array(
				'para_string' => 'all' ,
				'topage' => '1'
				);
			$snoopy->submit($hist_url , $submit_vars);

			//超时返回
			if ($snoopy->status != '200' || $snoopy->timed_out){
				return Response::json(['error'=>true , 'result'=>'tokenTimeOut' , 'message'=>'身份信息已过期，请重新登录5'] , 200);
			}

		    $result = $snoopy->results;

		    $html = new simple_html_dom();
		    $html->load($result);

		    $hist_arr = array();
			$table_line_1 = $html->find(".table_line");
			if( count($table_line_1) > 0 ){
				$table_line = $table_line_1[0];
			    $all_trs = $table_line->find("tr");

			    for( $i=1 ; $i<count($all_trs) ; $i++ ){
			    	$temp_tr = $all_trs[$i];
			    	$all_tds = $temp_tr->find("td");
			    	//分析详情连接
					$temp_as = $all_tds[2]->find("a");
					if( count($temp_as) > 0 ){
						$temp_as_href = $temp_as[0]->href;
						$target_detail_url = substr($temp_as_href , 8);
					}else{
						$target_detail_url = "";
					}
			    	//条码
			    	$num = $all_tds[1]->innertext;
			    	//标题
			    	$title = $all_tds[2]->innertext;
			    	//作者
			    	$author = $all_tds[3]->innertext;
			    	//借阅日期
			    	$rent_time = $all_tds[4]->innertext;
			    	//归还日期
			    	$return_time = $all_tds[5]->innertext;
			    	//馆藏地
			    	$location = $all_tds[6]->innertext;

					// $target_detail_url_arr = explode('/' , $target_detail_url);
					// if( count($target_detail_url) > 0 ){
					// 	$target_detail_url = $target_detail_url_arr[count($target_detail_url)-1];
					// }
			    	$hist_arr[] = array(
			    		'book_href' => $target_detail_url ,
			    		'book_name' => $title ,
			    		'book_author' => $author ,
			    		'rent_time' => $rent_time ,
			    		'return_time' => $return_time ,
			    		'book_location' => $location ,
						'book_code' => $num
			    		);
			    }
			    // DB::table('user_rent_history')->where('user_id','=',$user_id)->delete();
				// DB::table('user_rent_history')->insert($hist_arr);

                $return['history_arr'] = $hist_arr;
			}
		}


	    //**
	    // 获取此人的rss令牌
	    // 先判断是否已存在rss令牌
	    $rss_token = "";
	    //$target_rss_exit = DB::table('user')->where('id','=',$user_id)->get();

	    //if( count($target_rss_exit) == 0 || $target_rss_exit[0]->rss == "" || $target_rss_exit[0]->rss == null ){
		    $snoopy3 = new Snoopy;
		    $snoopy3->host = $library_ip;
		    $snoopy3->agent = $agent;
		    $snoopy3->cookies['PHPSESSID'] = $sessionid;
		    $snoopy3->fetch("http://202.206.242.99/reader/redr_cust_result.php");
		    $result3 = $snoopy3->results;
		    if( !$snoopy3->timed_out ){
			    $temp_pos1 = strpos($result3 , "http://202.206.242.99:80/reader/rss.php?type=3&id=");
			    //DB::table('user')->where('id','=',$user_id)->update( array('rss'=>$result3) );
			    if( $temp_pos1 ){
				    $target_url_string = substr($result3 , $temp_pos1+50 , 48);
				    //DB::table('user')->where('id','=',$user_id)->update( array('rss'=>$target_url_string) );
				    $rss_token = $target_url_string;
			    }
		    }
			//DB::table('user')->where('id','=',$user_id)->update(array('rss'=>$rss_token));
            $return['return_arr']['rss'] = $rss_token;

	    //}else{
	    //	$rss_token = $target_rss_exit[0]->rss;
	    //}


        return Response::json(['error'=>false , 'result'=>$return , 'message'=>'同步成功'] , 200);

    }


}
